<!--  -->
<template>
  <div class="main">
    <h2>服务项目管理</h2>
    <el-button type="primary" style="margin-left:20px" @click="handleOpenDialog('添加服务项目',1)">添加服务项目</el-button>
    <div id="search-card">
      <div id="search-content">
        <el-form ref="search" :model="search" :inline="true" :rules="rules">

          <el-form-item label="服务项目名称">
            <el-input v-model="search.lessonName" />
          </el-form-item>

          <el-form-item label="所属机构">
            <el-select v-model="search.campus" placeholder="请选择">
              <el-option
                v-for="item in campuslist"
                :key="item.campusId"
                :value="item.campusId"
                :label="item.campusName"
              />
            </el-select>
          </el-form-item>

          <el-form-item label="价格区间" prop="minPrice">
            <el-input v-model="search.minPrice" style="width:100px" maxlength="5" onkeyup="this.value=this.value.replace(/[^\d.]/g,'');" />
          </el-form-item>
          -
          <el-form-item prop="maxPrice">
            <el-input v-model="search.maxPrice" style="width:100px" maxlength="5" onkeyup="this.value=this.value.replace(/[^\d.]/g,'');" />
          </el-form-item>

          <el-form-item>
            <el-button type="primary" style="margin-left:10px" @click="conditionQuery('search')">查找</el-button>
            <el-button icon="el-icon-refresh" circle @click="refresh()" />
          </el-form-item>

          <!-- <el-form-item label="">
            <el-input v-model="mohu" />
          </el-form-item>

          <el-form-item>
            <el-button type="primary" style="margin-left:10px" @click="mohuQuery">查找</el-button>
            <el-button icon="el-icon-refresh" circle @click="fetchData()" />
          </el-form-item> -->
        </el-form>
      </div>

    </div>
    <div id="table-area">
      <el-table
        v-loading="listLoading"
        :data="list.slice((currentPage-1)*pagesize,currentPage*pagesize)"
        element-loading-text="Loading"
        border
        fit
        highlight-current-row
      >
        <!-- <el-table-column
          align="center"
          prop="lessonId"
          label="服务项目编号"
          min-width="5%"
        /> -->

        <el-table-column
          label="序号"
          type="index"
          min-width="5%"
          align="center"
        >
          <template scope="scope">
            <span>{{ (currentPage - 1) * pagesize + scope.$index + 1 }}</span>
          </template>
        </el-table-column>

        <el-table-column
          align="center"
          prop="campusName"
          label="所属机构"
          min-width="20%"
        />

        <el-table-column
          align="center"
          prop="lessonName"
          label="服务项目名称"
          min-width="20%"
        />

        <el-table-column
          align="center"
          prop="lessonImg"
          label="服务项目图片"
          min-width="10%"
        >
          <template slot-scope="Scope">
            <img :src="Scope.row.lessonImg" width="100px" height="60px">
          </template>
        </el-table-column>

        <el-table-column
          align="center"
          prop="lessonPrice"
          label="服务项目价格"
          min-width="10%"
        >
          <template slot-scope="Scope">
            <span>￥{{ Scope.row.lessonPrice/100 }}</span>
          </template>
        </el-table-column>

        <!-- <el-table-column
          align="center"
          prop="typeName"
          label="服务项目类别"
          min-width="9%"
        /> -->

        <el-table-column
          align="center"
          prop="lessonState"
          label="服务项目状态"
          min-width="10%"
        >
          <template slot-scope="Scope">
            <el-tag v-if="Scope.row.lessonState===0" type="warning">下架</el-tag>
            <el-tag v-if="Scope.row.lessonState===1" type="success">上架</el-tag>
          </template>
        </el-table-column>

        <el-table-column align="center" label="操作" min-width="25%">
          <template slot-scope="Scope">
            <el-button type="primary" icon="el-icon-edit" @click="handleOpenDialog('修改服务项目',2,Scope.row.lessonId)">查看/修改</el-button>
            <el-button type="danger" icon="el-icon-delete" @click="handleDelete(Scope.row.lessonId)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

    </div>
    <div id="pages">
      <el-pagination
        :current-page="currentPage"
        :page-sizes="[5,10]"
        :page-size="pagesize"
        layout="total, sizes,prev, pager, next"
        :total="list.length"
        prev-text="上一页"
        next-text="下一页"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>

    <classDetail :id="id" :title="title" :type="type" :visible="dialogFormVisible" @close="handleClose" />

  </div>

</template>

<script>
// 这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
// 例如：import 《组件名称》 from '《组件路径》';
import { getAll, deleteById, conditionSearch, mohuSearch } from '@/api/class'
import { getAllCampus } from '@/api/campus'
import classDetail from '@/views/class/classDetail'
export default {
  name: 'Class',
  // import引入的组件需要注入到对象中才能使用
  components: {
    'classDetail': classDetail
  },
  data() {
    // 这里存放数据
    return {
      currentPage: 1, // 默认显示页面为1
      pagesize: 5, //    每页的数据条数
      list: [],
      listLoading: false,
      classType: {},
      editType: 1, // 1 添加 2 修改
      url: process.env.VUE_APP_BASE_API + 'file/',
      id: '',
      title: '',
      type: 1,
      dialogFormVisible: false,
      search: {
        minPrice: '',
        maxPrice: ''
      },
      mohu: '',
      campuslist: {},
      rules: {
        minPrice: [
          { validator: this.validateComPrice, trigger: 'blur' },
          { validator: this.validateMinPrice, trigger: 'blur' }
        ],
        maxPrice: [
          { validator: this.validateComPrice, trigger: 'blur' },
          { validator: this.validateMaxPrice, trigger: 'blur' }
        ]
      }
    }
  },
  // 监听属性 类似于data概念
  computed: {},
  // 监控data中的数据变化
  watch: {},
  // 生命周期 - 创建完成（可以访问当前this实例）
  created() {
    this.fetchData()
    this.getAllCampus()
  },
  // 生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {},
  beforeCreate() {}, // 生命周期 - 创建之前
  beforeMount() {}, // 生命周期 - 挂载之前
  beforeUpdate() {}, // 生命周期 - 更新之前
  updated() {}, // 生命周期 - 更新之后
  beforeDestroy() {}, // 生命周期 - 销毁之前
  destroyed() {}, // 生命周期 - 销毁完成
  activated() {},
  // 方法集合
  methods: {
    // 每页下拉显示数据
    handleSizeChange: function(size) {
      this.pagesize = size
    },
    // 点击第几页
    handleCurrentChange: function(currentPage) {
      this.currentPage = currentPage
    },
    handleClick(row) {
      console.log(row)
    },
    fetchData() {
      getAll().then((resp) => {
        this.list = resp.obj
      })
    },
    refresh() {
      this.search = {}
      this.fetchData()
    },
    handleDelete(id) {
      this.$confirm('确认删除该条数据吗?').then(() => {
        deleteById(id).then(() => {
          this.$notify({
            title: '操作成功',
            type: 'success'
          })
          const totalPage = Math.ceil((this.list.length - 1) / this.pagesize)
          const page = this.currentPage > totalPage ? totalPage : this.currentPage
          this.currentPage = this.currentPage < 1 ? 1 : page
          this.fetchData()
        })
      })
    },
    handleClose() {
      this.dialogFormVisible = false
    },
    handleOpenDialog(title, type, id) {
      this.title = title
      this.type = type
      if (type === 2) { this.id = id }
      this.dialogFormVisible = true
    },
    getAllCampus() {
      getAllCampus().then((resp) => {
        this.campuslist = resp.obj
        console.log(this.campuslist)
      })
    },
    conditionQuery(search) {
      this.$refs[search].validate((valid) => {
        if (valid) {
          conditionSearch(this.search).then((data) => {
            this.list = data.obj
            this.currentPage = 1
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    mohuQuery() {
      console.log(this.mohu)
      mohuSearch(this.mohu).then((data) => {
        this.list = data.obj
        this.currentPage = 1
      })
    },
    validateComPrice(rule, value, callback) {
      const one = Number(value)
      if (one < 0) {
        return callback(new Error('价格必须大于0'))
      } else if (one > 99999) {
        return callback(new Error('价格必须小于99999'))
      }
      return callback()
    },
    validateMinPrice(rule, value, callback) {
      const one = Number(value)
      const max = Number(this.search.maxPrice)
      if (!max || one < max) {
        return callback()
      }
      return callback(new Error('不得大于最大值'))
    },
    validateMaxPrice(rule, value, callback) {
      const one = Number(value)
      const min = Number(this.search.minPrice)
      if (!min || one > min) {
        return callback()
      }
      return callback(new Error('不得小于最小值'))
    }
  } // 如果页面有keep-alive缓存功能，这个函数会触发
}
</script>
<style  scoped>
 #main{
        margin-left: 5px;
        margin-right: 5px;
    }

    #table-area{

        margin-left: 10px;
        margin-right: 10px;
    }
#search-card {
  line-height: 35px;
  text-align: right;
  width: 100%;
  height: 70px;
  overflow-x: hidden;
}
</style>
